import Vue from 'vue'
import Router from 'vue-router'
import iView from 'iview'
import Cookies from 'js-cookie'
import util from '../libs/util'
import app from '../store/modules/app'
Vue.use(Router)
import { routers, otherRouter, appRouter } from './router'

const RouterConfig = {
    routes: routers
}

export const router = new Router(RouterConfig)

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start()
    util.title(to.meta.title)
    if (to.name === 'login' && Cookies.get('uuid')) {
        next({
            name: 'home_index'
        })
    } else if (!Cookies.get('uuid') && to.name !== 'login') {
        next({
            name: 'login'
        })
    } else if (to.name == 'error-403' || to.name == 'error-404') {
        next()
    } else if (to.name !== 'login' && Cookies.get('uuid')) {
        if (to.name == 'home_index') {
            next()
        } else {
            const newMenuRouter = !!app.state.newMenuRouter.length ? app.state.newMenuRouter : !!localStorage.menuList ? JSON.parse(localStorage.menuList) : [];
            const curRouterObj = util.getRouterObjByName(newMenuRouter, to.name);
            if (!curRouterObj) {
                const btnpower = JSON.parse(localStorage.btnPower);
                if (btnpower.includes('khgli_xgpz')) {
                    next()
                } else {
                    next({
                        replace: true,
                        name: 'error-403'
                    });
                }

            } else {
                util.toDefaultPage(newMenuRouter, to.name, router, next);
                //next() 
            }
        }
    } else {
        next()
    }
    // next()



})

router.afterEach((to) => {
    iView.LoadingBar.finish()
    window.scrollTo(0, 0)
})